<?php
    require("../config.php");
    if(!(isset($_REQUEST["key"]) && isset($_REQUEST["to"]) && isset($_REQUEST["title"]) && isset($_REQUEST["text"]) && isset($_REQUEST["name"]))){
        $bluestar->add_log($conn,"调用发送邮件api:缺少请求参数",$row["user_id"],$ip,$time,"403");//插入日志
        $return = array('info'=>"请检查请求参数，存在缺少", 'code'=>403);
        die(json_encode($return));
    }
    //获取当前文件的绝对路径
    $current_file_path = dirname(__FILE__);
    $absolute_file_path = realpath($current_file_path);
    //echo $absolute_file_path;

    //定义程序文件路径
    define("_APP_INSTALL", $absolute_file_path."/app/install/");
    define("_APP_LOGIN", $absolute_file_path."/app/login/");
    define("_APP_SIGNUP", $absolute_file_path."/app/signup/");
    define("_APP_SMTP", $absolute_file_path."/app/phpmailer/");

    require("./function.php");
    $conn = mysqli_connect($host, $user, $password, $dbname);
    $appid = preg_replace('/[^A-Za-z0-9]/', '', $_REQUEST["appid"]);
    $hash = $_REQUEST["key"];
    $to = $_REQUEST["to"];
    $text = $_REQUEST["text"];
    $title = $_REQUEST["title"];
    $nickname = $_REQUEST["name"];
    $ip = $_SERVER['REMOTE_ADDR'];
    $time = time();
    $result = mysqli_query($conn,"SELECT * FROM blue_service WHERE appid='$appid'");
    if($result->num_rows ==0){//不存在当前服务
        $bluestar->add_log($conn,"调用发送邮件api：不存在当前appid",$row["user_id"],$ip,$time,"404");//插入日志
        $return = array('info'=>"请检查appid", 'code'=>404);
        echo json_encode($return);
    }else{
        while($row = mysqli_fetch_array($result)){
            if(hash("sha256", $title.$to.$nickname.$text.$row["token"]) == $hash){//允许发送
                $config = $bluestar->get_config($conn);
                $bluestar->sendMail($to,$nickname,$title,$text, $config);
                $text_hash = base64_encode($text);
                $bluestar->add_log($conn,"调用发送邮件api：to-$to;title-$title;text-$text_hash",$row["user_id"],$ip,$time,"200");//插入日志
            }else{
                $bluestar->add_log($conn,"调用发送邮件api：key验证不通过",$row["user_id"],$ip,$time,"401");//插入日志
                $return = array('info'=>"请检查key", 'code'=>401);
                echo json_encode($return);
            }
        }
    }

?>